package com.cameras;

import interface_design.*;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Gallery;
import android.widget.GridView;
import android.widget.AdapterView.OnItemClickListener;

public class Album extends Activity {

	private Gallery myGallery;

	/**
	 * Cursor used to access the results from querying for images on the SD
	 * card.
	 */
	private Cursor cursor;
	/*
	 * Column index for the Thumbnails Image IDs.
	 */
	private int columnIndex;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sdcard);

		// Set up an array of the Thumbnail Image ID column we want
		String[] projection = { MediaStore.Images.Thumbnails._ID };
		// Create the cursor pointing to the SDCard
		cursor = managedQuery(
				MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, projection, // Which
																				// columns
																				// to
																				// return
				null, // Return all rows
				null, MediaStore.Images.Thumbnails.IMAGE_ID);
		// Get the column index of the Thumbnails Image ID
		columnIndex = cursor
				.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);

		GridView sdcardImages = (GridView) findViewById(R.id.sdcard);
		sdcardImages.setAdapter(new PictureGridAdapter(this, cursor,
				columnIndex));

		// Set up a click listener
		sdcardImages.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView parent, View v, int position,
					long id) {
				// Get the data location of the image
				String[] projection = { MediaStore.Images.Media.DATA };
				cursor = managedQuery(
						MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
						projection, // Which columns to return
						null, // Return all rows
						null, null);
				columnIndex = cursor
						.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
				cursor.moveToPosition(position);
				// Get image filename
				String imagePath = cursor.getString(columnIndex);
				// Use this path to do further processing, i.e. full screen
				// display
			}
		});

		// myGallery = (Gallery) findViewById(R.id.myGallery);
		// myGallery.setAdapter(new GalleryAdapter(this));
	}

}
